package com.lishe.lishe.autoScrollListView;

import android.content.Context;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


import com.lishe.lishe.R;
import com.lishe.lishe.lishe.module.excitation.bean.EncourageObject;
import com.lishe.lishe.lishe.utils.TimeUtils;

import java.util.List;
import java.util.Random;

/**
 * @Author peerless2012
 * @Email peerless2012@126.com
 * @DateTime 2016年5月14日 下午10:48:51
 * @Version V1.0
 * @Description: 自动滚动适配器
 */
public class AutoScrollAdapter extends BaseAdapter implements AutoScrollListView.AutoScroll {

    private Random random = new Random();
    private LayoutInflater mLayoutInflater;

    private List<EncourageObject> mList;

    public AutoScrollAdapter(List<EncourageObject> list) {
        mList = list;
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            if (mLayoutInflater == null) {
                mLayoutInflater = LayoutInflater.from(parent.getContext());
            }
            convertView = mLayoutInflater.inflate(R.layout.item_excitation_team_excitation, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.content = (TextView) convertView.findViewById(R.id.content);
            viewHolder.time = (TextView) convertView.findViewById(R.id.time);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
//		convertView.setBackgroundColor(Color.argb(100, random.nextInt(255), random.nextInt(255), random.nextInt(255)));
        viewHolder.content.setText(mList.get(position).getNewsContent());
        if (!TextUtils.isEmpty(mList.get(position).getSendTime())) {
            String time0 = TimeUtils.longToyyyyMMdd(Long.parseLong(mList.get(position).getSendTime()));
            int index = time0.indexOf("-");
            String time = time0.substring(index + 1);
            time = time.replace("-", "月") + "日";
            viewHolder.time.setText(time);
        }
        return convertView;
    }

    @Override
    public int getListItemHeight(Context context) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, context.getResources().getDisplayMetrics());
    }

    @Override
    public void updateDataChanged(BaseAdapter adapter) {
        if (adapter != null)
            adapter.notifyDataSetChanged();
    }

    @Override
    public int getImmovableCount() {
        return scrollCount;
    }

    class ViewHolder {
        public TextView content;
        public TextView time;
    }
    int scrollCount=3;
    public void setScrollCount(int count){
        scrollCount=count;
        notifyDataSetChanged();
    }

}
